package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.domain.User;
import org.example.entity.R;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author TonySong
 * @date 2024/6/27 0027
 * @time 16:33
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Value("${server.port}")
    private Integer port;
    @Resource
    private UserService userService;

    @GetMapping("/users")
    public R getUsers(@RequestParam(name="offset",defaultValue = "1") Integer offset,
                      @RequestParam(name="size",defaultValue = "5",required = false)Integer size){
        log.info("---------------->port:{}",port);
        return R.success(this.userService.page(new Page<>(offset,size)));
    }

    @PostMapping("/")
    public R addUser(@RequestBody User user){
        return R.success(this.userService.save(user));
    }
    @DeleteMapping("/{id}")
    public R deleteUser(@PathVariable("id") Integer id) {
        return R.success(this.userService.removeById(id));
    }

    @PutMapping("/")
    public R updateUser(@RequestBody User user) {
        return R.success(this.userService.updateById(user));
    }

    @GetMapping("/{id}")
    public R getUser(@PathVariable("id") Integer id) {
        return R.success(this.userService.getById(id));
    }
}